import { _decorator, Component, instantiate, math, Prefab, UITransform, view, Node, Input, input, AudioClip } from "cc";
import { SetBasicInfo } from "../Utils/SetBasicInfo";
import { GameManager } from "./GameManager";
import { Enemy } from "./Enemy";
import { AudioMgr } from "./AudioMgr";
const { ccclass, property } = _decorator;

@ccclass("EnemyManager")
export class EnemyManager extends Component {

  private static instance: EnemyManager = null;
  public static getInstance(): EnemyManager {
    if (!this.instance) this.instance = new EnemyManager();
    return this.instance;
  }

  pageWidth: number = 0;
  pageHeight: number = 0;

  // 搁多久生成一个敌人
  @property
  enemy0SpawnRate: number = 1;

  @property(Prefab)
  enemy0Prefab: Prefab = null;

  // 搁多久生成一个敌人
  @property
  enemy1SpawnRate: number = 3;

  @property(Prefab)
  enemy1Prefab: Prefab = null;

  // 搁多久生成一个敌人
  @property
  enemy2SpawnRate: number = 10;

  @property(Prefab)
  enemy2Prefab: Prefab = null;

  // 搁多久生成一个奖励
  @property
  rewardSpawnRate: number = 15;

  @property({ type: Prefab, tooltip: "双发子弹奖励" })
  reward1Prefab: Prefab = null;
  @property({ type: Prefab, tooltip: "炸弹奖励" })
  reward2Prefab: Prefab = null;

  @property([Node])
  enemyList: Node[] = [];

  @property(AudioClip)
  useBombAudio: AudioClip = null

  // 双击检测时间间隔
  doubleClickInterval: number = 0.2;
  // 上次点击时间
  lastClickTime: number = 0;

  protected onLoad(): void {
    this.lastClickTime = 0;
    input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  }

  start() {
    EnemyManager.instance = this
    this.getBgHeight();
    this.schedule(() => this.createEnemy(this.enemy0Prefab), this.enemy0SpawnRate);
    this.schedule(() => this.createEnemy(this.enemy1Prefab), this.enemy1SpawnRate);
    this.schedule(() => this.createEnemy(this.enemy2Prefab), this.enemy2SpawnRate);
    this.schedule(this.createReward, this.rewardSpawnRate);
  }

  update(deltaTime: number) {}

  protected onDestroy(): void {
    this.unschedule(this.objectSpawn);
    this.unschedule(this.createReward);
    this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  }

  /**
   * 生成敌人
   * @param enemyPrefab 预制体
   */
  createEnemy(enemyPrefab: Prefab) {
    // 克隆节点
    const enemy = instantiate(enemyPrefab);
    // 获取位置 页面高度一半 - 节点高度一半
    const nodeSize = enemy.getComponent(UITransform).contentSize;
    // 获取节点尺寸
    const width_half = this.pageWidth / 2 - nodeSize.height / 2;

    this.objectSpawn(enemy, -width_half, width_half, this.pageHeight);
    this.enemyList.push(enemy);
  }

  /**
   * 生成奖励
   * @param rewardPrefab 预制体
   */
  createReward() {
    console.log("生成奖励");
    // 生成 1 2
    const randomNumber = math.randomRangeInt(1, 3);
    let prefab: Prefab = this[`reward${randomNumber}Prefab`];
    // 克隆节点
    const enemy = instantiate(prefab);
    // 获取位置 页面高度一半 - 节点高度一半
    const nodeSize = enemy.getComponent(UITransform).contentSize;
    // 获取节点尺寸
    const width_half = this.pageWidth / 2 - nodeSize.height / 2;
    return this.objectSpawn(enemy, -width_half, width_half, this.pageHeight);
  }

  /**
   * 生成敌人/奖励
   * @param enemyPrefab 敌人/奖励 预制体 {@link Prefab}
   */
  objectSpawn(enemy: Node, minX: number, maxX: number, Y: number) {
    // 将节点添加到当前节点
    this.node.addChild(enemy);
    // 生成随机位置
    const randomX = math.randomRangeInt(minX, maxX);
    // 设置位置
    SetBasicInfo.setPosition(enemy, {
      x: randomX,
      y: Y,
    });
  }

  getBgHeight() {
    const { width, height } = view.getVisibleSize();
    this.pageWidth = width;
    this.pageHeight = height;
  }

  onTouchEnd() {
    let currentTime = Date.now();
    let timeDiff = (currentTime - this.lastClickTime) / 1000; // 转换成秒

    if (timeDiff < this.doubleClickInterval) {
      this.onDoubleClick();
    }

    this.lastClickTime = currentTime;
  }

  onDoubleClick() {
    if (!GameManager.getInstance().isHaveBomb) return;

    GameManager.getInstance().useBomb();

    AudioMgr.inst.playOneShot(this.useBombAudio);

    this.enemyList.forEach((enemy) => {
      enemy.getComponent(Enemy).killNow();
    })
  }

  removeEnemy(n: Node) {
    const index = this.enemyList.indexOf(n);
    if (index > -1) {
      this.enemyList.splice(index, 1);
    }
  }
}
